/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.web.product;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.hk.shop.entity.brand.HkShopBrand;
import com.thinkgem.jeesite.modules.hk.shop.entity.category.HkShopCategory;
import com.thinkgem.jeesite.modules.hk.shop.service.brand.HkShopBrandService;
import com.thinkgem.jeesite.modules.hk.shop.service.category.HkShopCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.shop.entity.product.HkShopProduct;
import com.thinkgem.jeesite.modules.hk.shop.service.product.HkShopProductService;

import java.util.List;

/**
 * 乐购商品Controller
 * @author 罗天文
 * @version 2018-05-16
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/product/hkShopProduct")
public class HkShopProductController extends BaseController {

	@Autowired
	private HkShopProductService hkShopProductService;
	@Autowired
	private HkShopBrandService hkShopBrandService;

	@ModelAttribute
	public HkShopProduct get(@RequestParam(required=false) String id) {
		HkShopProduct entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hkShopProductService.get(id);
		}
		if (entity == null){
			entity = new HkShopProduct();
		}
		return entity;
	}



	@RequiresPermissions("shop:product:hkShopProduct:view")
	@RequestMapping(value = {"list", ""})
	public String list(HkShopProduct hkShopProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("brands", hkShopBrandService.findList(new HkShopBrand()));
		Page<HkShopProduct> page = hkShopProductService.findPage(new Page<HkShopProduct>(request, response), hkShopProduct); 
		model.addAttribute("page", page);
		return "hk/shop/product/hkShopProductList";
	}

	@RequiresPermissions("shop:product:hkShopProduct:view")
	@RequestMapping(value = "form")
	public String form(HkShopProduct hkShopProduct, Model model) {
		model.addAttribute("brands", hkShopBrandService.findList(new HkShopBrand()));
		model.addAttribute("hkShopProduct", hkShopProduct);
		return "hk/shop/product/hkShopProductForm";
	}

	@RequiresPermissions("shop:product:hkShopProduct:edit")
	@RequestMapping(value = "save")
	public String save(HkShopProduct hkShopProduct, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hkShopProduct)){
			return form(hkShopProduct, model);
		}
		if(null==hkShopProduct.getHkShopProductPropertyList()||hkShopProduct.getHkShopProductPropertyList().size()==0){
			addMessage(model, "商品属性必须选择");
			return form(hkShopProduct, model);
		}
        if(null==hkShopProduct.getHkShopProductImagesList()||hkShopProduct.getHkShopProductImagesList().size()==0){
            addMessage(model, "产品图片必须选择");
            return form(hkShopProduct, model);
        }
		hkShopProductService.save(hkShopProduct);
		addMessage(redirectAttributes, "保存乐购商品成功");
		return "redirect:"+Global.getAdminPath()+"/shop/product/hkShopProduct/?repage";
	}
	
	@RequiresPermissions("shop:product:hkShopProduct:edit")
	@RequestMapping(value = "delete")
	public String delete(HkShopProduct hkShopProduct, RedirectAttributes redirectAttributes) {
		hkShopProductService.delete(hkShopProduct);
		addMessage(redirectAttributes, "删除乐购商品成功");
		return "redirect:"+Global.getAdminPath()+"/shop/product/hkShopProduct/?repage";
	}

}